using UnityEngine;
using UnityEngine.EventSystems;

public class Bird : MonoBehaviour, IPointerClickHandler
{
    [Header("飞行速度")]
    [SerializeField] float flySpeed = 4.0f;
    [Header("飞行速度系数")]
    [SerializeField] float flySpeedScale = 5.0f;
    protected Rigidbody2D rigidbodyXD;
    private Slingshot slingshot;
    public BirdStatus Status { get; set; }
    private bool hitted = false;

    public BirdType BirdType { get; set; }


    void Awake()
    {
        this.slingshot = GameObject.Find(Defines.Slingshot).GetComponent<Slingshot>();
        this.rigidbodyXD = GetComponent<Rigidbody2D>();
        this.OtherLoad();
    }

    protected virtual void OtherLoad()
    {

    }

    void Start()
    {

    }

    void Update()
    {
        switch (this.Status)
        {
            case BirdStatus.Idle:
                break;
            case BirdStatus.Ready:
                transform.position = this.GetPosition();
                if (Input.GetMouseButtonUp(0))
                {
                    this.Go();
                }
                break;
            case BirdStatus.Go:
                if (this.rigidbodyXD.velocity.magnitude <= 0)
                {
                    this.Finish();
                }
                else
                {
                    Common.CameraFollow(transform.position);
                }
                if (Input.GetMouseButtonUp(0))
                {
                    this.UseSkill();
                }
                break;
        }
    }

    void Go()
    {

        this.Status = BirdStatus.Go;
        GameManager.Instance.birdFlying = true;
        // 弹弓
        this.slingshot.EndDraw();
        // 速度
        this.rigidbodyXD.bodyType = RigidbodyType2D.Dynamic;
        var scale = (this.slingshot.CenterPointPosition - transform.position).magnitude % this.flySpeedScale;
        this.rigidbodyXD.velocity = (this.slingshot.CenterPointPosition - transform.position).normalized * flySpeed * scale;
        // 声音
        AudioManager.Instance.PlayBirdFly(transform.position);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (!GameManager.Instance.birdFlying && this.Status == BirdStatus.Idle)
        {
            this.slingshot.StartDraw(transform);
            Invoke(nameof(ForReady), 0.01f);
            AudioManager.Instance.PlayBirdReady(transform.position);
        }
    }

    private void ForReady()
    {
        this.Status = BirdStatus.Ready;
    }

    private Vector3 GetPosition()
    {
        var mousePosition = Utility.GetMouseWorldPoint();
        var centerPosition = this.slingshot.CenterPointPosition;

        if (Vector3.Distance(mousePosition, centerPosition) > this.slingshot.MaxDistance)
        {
            return (mousePosition - centerPosition).normalized * this.slingshot.MaxDistance + centerPosition;
        }
        else
        {
            return mousePosition;
        }
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (this.Status == BirdStatus.Go)
        {
            this.hitted = true;

            if (other.relativeVelocity.magnitude > 2)
            {
                AudioManager.Instance.PlayBirdHit(transform.position);
            }
        }
    }

    private void UseSkill()
    {
        switch (this.BirdType)
        {
            case BirdType.BigMouth:
            case BirdType.Acceleratable:
                if (this.hitted)
                    // 已经击中了就不能再使用飞行技能
                    return;
                this.FlySkill();
                break;
            case BirdType.SelfDestruct:
                this.BoomSkill();
                break;
        }
    }

    /// <summary>
    /// 飞行技能
    /// </summary>
    protected virtual void FlySkill()
    {

    }

    /// <summary>
    /// 爆炸技能
    /// </summary>
    protected virtual void BoomSkill()
    {

    }

    protected void Finish()
    {
        this.Status = BirdStatus.Finish;
        GameManager.Instance.FlyFinished();
    }
}
